package com.ruoyi.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.domain.ProducePlan;
import com.ruoyi.mapper.RigMapper;
import com.ruoyi.domain.Rig;
import com.ruoyi.service.IRigService;

/**
 * 作业装配Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class RigServiceImpl implements IRigService 
{
    @Autowired
    private RigMapper rigMapper;

    /**
     * 查询作业装配
     * 
     * @param id 作业装配主键
     * @return 作业装配
     */
    @Override
    public Rig selectRigById(String id)
    {
        return rigMapper.selectRigById(id);
    }

    /**
     * 查询作业装配列表
     * 
     * @param rig 作业装配
     * @return 作业装配
     */
    @Override
    public List<Rig> selectRigList(Rig rig)
    {
        return rigMapper.selectRigList(rig);
    }

    /**
     * 新增作业装配
     * 
     * @param rig 作业装配
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRig(Rig rig)
    {
        int rows = rigMapper.insertRig(rig);
        insertProducePlan(rig);
        return rows;
    }

    /**
     * 修改作业装配
     * 
     * @param rig 作业装配
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRig(Rig rig)
    {
        rigMapper.deleteProducePlanByRigId(rig.getId());
        insertProducePlan(rig);
        return rigMapper.updateRig(rig);
    }

    /**
     * 批量删除作业装配
     * 
     * @param ids 需要删除的作业装配主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRigByIds(String[] ids)
    {
        rigMapper.deleteProducePlanByRigIds(ids);
        return rigMapper.deleteRigByIds(ids);
    }

    /**
     * 删除作业装配信息
     * 
     * @param id 作业装配主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRigById(String id)
    {
        rigMapper.deleteProducePlanByRigId(id);
        return rigMapper.deleteRigById(id);
    }

    /**
     * 新增排产计划信息
     * 
     * @param rig 作业装配对象
     */
    public void insertProducePlan(Rig rig)
    {
        List<ProducePlan> producePlanList = rig.getProducePlanList();
        String id = rig.getId();
        if (StringUtils.isNotNull(producePlanList))
        {
            List<ProducePlan> list = new ArrayList<ProducePlan>();
            for (ProducePlan producePlan : producePlanList)
            {
                producePlan.setRigId(id);
                list.add(producePlan);
            }
            if (list.size() > 0)
            {
                rigMapper.batchProducePlan(list);
            }
        }
    }
}
